import { observable, action, toJS } from 'mobx';
import { ListViewPage } from 'weaver-mobile-page';

import * as Api from '../../api/HisBlogApi';

import BlogCommentStore from '../comment/BlogCommentStore'

export default class HisBlogStore {

  @observable blogInfo = undefined; // 他的个人信息（包含统计信息 和 查看权限情况）
  @observable uid = ''; // 他的微博列表所需信息
  @observable hisEndDate = '';
  @observable hisBlog = new BlogCommentStore({ getComment: this.getHisBlog }); // 他的微博
  @observable viewBlogList = []; 
  @observable hisAttentionList = null; // 他的关注
  @observable hisFansList = null; //他的粉丝
  @observable isNoData = false; // 他的关注||他的粉丝是否无数据
  @observable isContentLoading = true;

  @observable child = null; 
  @observable childId = null;

  constructor(params) {
    // super(params);
    if (params && params.childId) {
      this.childId = params.childId;
    }
  }

  @action
  inital = (path) => {
    this.hisAttentionList = new ListViewPage.Store({ route: path, id:'hisblog' });
    this.hisFansList = new ListViewPage.Store({ route: path, id:'hisblog' });
  }
  
	@action('个人信息')
	getBlogPersonalInfo = (params) => {
    Api.getBlogPersonalInfo(params).then(res => {
      this.blogInfo = res;
		});
  }

  @action('他的微博')
  getHisBlog = () => {
    let params = {
      userId: this.uid,
      endDate: this.hisEndDate,
    }
    return Api.getViewBlogList(params).then(data => {
      if(data.status === '1' && data.viewRightStatus !== '0' && data.replyList.length !== 0) {
        this.hisEndDate = data.replyList[data.replyList.length-1].workdate.split('-');
        let temp = this.hisEndDate[this.hisEndDate.length-1]-1;
        this.hisEndDate[this.hisEndDate.length-1] = temp;
        this.hisEndDate = this.hisEndDate.join('-');
      }
      return data;
    })
  }

  @action('他的微博列表')
	getViewBlogList = (params) => {
		this.isContentLoading = true;
		Api.getViewBlogList(params).then( result => {
			this.viewBlogList = toJS(result.discussList);
			this.isContentLoading = false;
		});
  }

	@action('他的关注')
	getMyBlogAttention = (params) => {
    this.isNoData = false;
		Api.getMyBlogAttention(params).then( result => {
      const { status, attentionList } = result;
      if (status === '1') {
        if (attentionList && attentionList.length === 0) this.isNoData = true;
        else {
          const newState = {
            isTable: false,
            loading: false,
            haveCheck: true,
            template: {default: []},
            hasMore: false,
            data: [...attentionList],
          };
          this.hisAttentionList.setState(newState);
          this.hisAttentionList.updateDataSource();
        }
      }
		});
	}

	@action('他的粉丝')
	getAttentionMesUsers = (params) => {
    this.isNoData = false;
		Api.getAttentionMesUsers(params).then( result => {
      const { status, attnList } = result;
      if (status === '1') {
        if (attnList && attnList.length === 0) this.isNoData = true;
        else {
          const newState = {
            isTable: false,
            loading: false,
            haveCheck: true,
            template: {default: []},
            hasMore: false,
            data: [...attnList],
          };
          this.hisFansList.setState(newState);
          this.hisFansList.updateDataSource();
        }
      }
		});
  }
    
  @action('修改用户id')
  setUid = (val) => {
    this.uid = val;
  }

  @action('更新关注状态')
  updateHisAttentionStatus = (attnInfo) =>{
    let {userId, attentionStatus} = attnInfo;
    if (userId === this.blogInfo.userId) {
      return {...this.blogInfo, attentionStatus:attentionStatus}
    }
  }

  setEndDate = () => {
    this.hisEndDate = '';
  }

	@action('clear')
	clear = () => {
		this.blogInfo = undefined; 
		// this.viewBlogList = []; 
		this.hisAttentionList = [];
		this.hisFansList = [];
    this.isContentLoading = true;
    this.hisEndDate = '';
	}

  @action //给当前父目录设置其下的子目录，如果当前目录下没有子目录，则证明走到了目录的最底层，跳出路由循环
  setChild = (childId) => {
    this.child = new HisBlogStore({ childId });
    // this.child.getListWithchildId();
  }


    @action('添加特别关注')
    addSpecialUser =  (parms,callback) => {
        Api.addSpecialUsers(parms).then(result => {
            let data = Object.assign({}, this.blogInfo, { specialStatus: '2'})
            this.blogInfo = data;
        })
    }
    @action('取消特别关注')
    cancelSpecialUser = (params,callback) => {
        Api.cancelSpecialUsers(params).then(result => {
            let data = Object.assign({}, this.blogInfo, { specialStatus: '1'})
            this.blogInfo = data;
        })
    }

}